package com.p6spy.engine.config;

import cn.hutool.core.date.DateUtil;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;

import java.util.Date;

/**
 * SQL执行分析配置
 */
public class P6spySqlFormat implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String s4) {
        try {
            return !"".equals(sql.trim()) ? DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss:sss") + " | SQL: " + CCJSqlParserUtil.parse(sql).toString() + "; | " + " 耗时 " + elapsed + " ms " : "";
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }
    }

}
